package controller

import (
	"fmt"
	"jtvss/api/operation/dto"
	"jtvss/api/operation/model"
	"jtvss/middleware"
	"jtvss/pkg/ctx"
	"jtvss/pkg/orm"
	"jtvss/util"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Vehicle struct {
}

func (o *Vehicle) ListHandler(c *gin.Context) {
	var p dto.Where
	if err := c.ShouldBind(&p); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	p.OrganizeGUID = middleware.UserToken(c).OrganizeGUID
	var data []model.Vehicle
	total, _ := p.DbModelWhere(&model.Vehicle{}).Find(&data)
	ctx.JSONWriteTotal(total, data, c)
}

// GetHandler 获取指定id
func (o *Vehicle) GetHandler(c *gin.Context) {
	util.QueryByID(&model.Vehicle{}, c)
}

// AddHandler 新增
func (o *Vehicle) AddHandler(c *gin.Context) {
	var p model.Vehicle
	//获取参数
	if err := c.ShouldBind(&p.VehicleOpt); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	p.GUID = util.UUID()
	p.OrganizeGUID = middleware.UserToken(c).OrganizeGUID
	if err := orm.DbCreate(&p); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ctx.JSONWriteData(nil, c)
}

type batchAdd struct {
	Prefix      string `json:"prefix"`
	StartNumber int    `json:"startNumber"`
	Count       int    `json:"count"`
	model.VehicleOpt
}

// BatchAddHandler 新增
func (o *Vehicle) BatchAddHandler(c *gin.Context) {
	var p batchAdd
	//获取参数
	if err := c.ShouldBind(&p); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	t := middleware.UserToken(c)
	lzero := len(strconv.Itoa(p.StartNumber + p.Count - 1))
	var data []model.Vehicle
	for i := 0; i < p.Count; i++ {
		v := model.Vehicle{}
		v.VehicleOpt = p.VehicleOpt
		v.DeviceNo = fmt.Sprintf("%s%0*d", p.Prefix, lzero, p.StartNumber+i)
		v.DeviceName = v.DeviceNo
		v.GUID = util.UUID()
		v.OrganizeGUID = t.OrganizeGUID
		data = append(data, v)
	}
	if err := orm.DbCreate(&data); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ctx.JSONWriteData(nil, c)
}

// UpdateHandler 修改
func (o *Vehicle) UpdateHandler(c *gin.Context) {
	var p model.Vehicle
	//获取参数
	if err := c.ShouldBind(&p.VehicleOpt); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	if err := orm.DbUpdateModel(&p); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ctx.JSONWriteData(nil, c)
}

// resetOrganize 更新
type resetOrganize struct {
	DeviceIds  string `json:"deviceIds"`
	OrganizeId int    `json:"organizeId"`
}

func (o *Vehicle) ResetOrganizeHandler(c *gin.Context) {
	var p resetOrganize
	//获取参数
	if err := c.ShouldBind(&p); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ids := util.StringSplit(p.DeviceIds, ",")
	if err := orm.DbUpdateByID(&model.Vehicle{}, ids, orm.H{"OrganizeID": p.OrganizeId}); err != nil {
		ctx.JSONWriteError(err, c)
		return
	}
	ctx.JSONWriteData(nil, c)
}

// DeleteHandler 删除
func (o *Vehicle) DeleteHandler(c *gin.Context) {
	util.Deletes(&model.Vehicle{}, c)
}

func (o Vehicle) Routers(r *gin.RouterGroup) {
	r.GET("/list", o.ListHandler)
	r.GET("/:id", o.GetHandler)
	r.POST("", o.AddHandler)
	r.POST("/batchAdd", o.BatchAddHandler)
	r.PUT("", o.UpdateHandler)
	r.PUT("/resetOrganize", o.ResetOrganizeHandler)
	r.DELETE("/:id", o.DeleteHandler)
}
